const app = getApp();
const {
  $resHost
} = app.globalData;
const defaultPng = `${$resHost}/img/miniprogram/home/default.png`;
Component({
  properties: {
    isNew: {
      type: Boolean,
      value: false
    }
  },
  data: {
    navList: [],
    navIndex: 0,
    classList: []
  },
  lifetimes: {
    attached() {
      // let useEntityGoodsClass = xhs.getStorageSync('config').wechatInfo.useEntityGoodsClass;

      let useEntityGoodsClass = '0'; //0为类目，1为店铺分类
      this.setData({
        useEntityGoodsClass
      }, () => {
        this.getClassList();
      });
    }
  },
  methods: {
    onSearch(e) {
      let url = `/pages/TabBar/Goods/${this.data.isNew ? 'new' : 'index'}?type=1&searchName=${e.detail.value}`;
      xhs.navigateTo({
        url
      });
    },
    changeNavId({
      currentTarget: {
        dataset: {
          index
        }
      }
    }) {
      if (index !== this.data.navIndex) {
        this.setData({
          navIndex: +index
        });
      }
    },
    getClassList() {
      // let url = this.data.useEntityGoodsClass == '1' ? '/user/goodsclassentity/getlist' : '/user/goodsclass/getlist';
      // app.$getSuccessData(url, {
      //   isAll: true
      // }).then(navList => {
      //   this.setData({
      //     navList: this.initData(navList)
      //   });
      // const that = this;
      // xhs.request({
      //   url: 'https://admin_yzyp.qqlife.com/goods/get-list', // 替换为实际的 API 地址
      //   method: 'GET',
      //   success(res) {
      //     if (res.statusCode === 200) {
      //       console.log('获取的商品分类数据:', res.data); // 打印获取的 data 内容
      //       that.setData({
      //         classList: res.data, // 将数据存储到页面 data 中
      //       });
      //     } else {
      //       console.error('请求失败:', res);
      //     }
      //   },
      //   fail(err) {
      //     console.error('请求出错:', err);
      //   },
      // });
    },
    goGoods({
      currentTarget: {
        dataset: {
          id
        }
      }
    }) {
      let url = `/pages/TabBar/Goods/${this.data.isNew ? 'new' : 'index'}?classId=${id}&type=1&useEntityGoodsClass=${this.data.useEntityGoodsClass}`;
      xhs.navigateTo({
        url: url.addPageShare()
      });
    },
    initData(list, deep = 1) {
      const result = [];
      list.forEach(item => {
        let obj = {};
        let url = '';
        if (this.data.useEntityGoodsClass == '1') {
          url = item.icon.thumb ? item.icon.thumb : defaultPng;
        } else {
          url = item.iconUrl[0] && item.iconUrl[0].thumb || defaultPng;
        }
        if (deep !== 3) {
          item.child = this.initData(item.child, deep + 1);
          if (deep == 1 && !item.child.length) return;
          obj = {
            id: item.id,
            className: item.className ? item.className.slice(0, 6) : item.name.slice(0, 6),
            child: item.child,
            iconUrl: url
          };
        } else {
          obj = {
            id: item.id,
            className: item.className ? item.className.slice(0, 6) : item.name.slice(0, 6),
            iconUrl: url
          };
        }
        result.push(obj);
      });
      return result;
    }
  }
});